package com.rtos.model.terminal;

import com.rtos.vo.file.diskPane.DiskViewer;
import com.rtos.vo.file.refresh.Refresh;
import com.rtos.model.fileSystem.FileUtil;
import com.rtos.vo.file.popFileContent.PopFileContent;
import javafx.scene.control.TreeView;

public class TerminalKernel {
    public static TerminalKernel terminalKernel;
    public String helpMessage = this.initHelpMessage();
    public DiskViewer diskViewer;
    public TerminalKernel() {}

    public static TerminalKernel getInstance() {
        if(terminalKernel == null) {
            terminalKernel = new TerminalKernel();
        }
        return terminalKernel;
    }

    public String process(String command, FileUtil fileUtil, TreeView FileTree,DiskViewer diskViewer) {
        //先去输入的命令两端多余的空格
        String trimCommand = command.trim();
        //判断输入的是什么类型的命令
        this.diskViewer=diskViewer;
        switch (this.getSegmentNumber(trimCommand)) {
            case 1:
                return this.process1LengthCommand(trimCommand);
            case 2:
                try {
                    return this.process2LengthCommand (trimCommand, fileUtil,FileTree);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            case 3:
                try {
                    return this.process3LengthCommand(trimCommand, fileUtil,FileTree);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            default:
                return "Unknow command length !";
        }

    }

    public String process1LengthCommand(String command) {
        //匹配当前命令
        if(this.isSame(command, "fuck")) {
            return "what the fuck ?";
        }else if (this.isSame(command, "help")) {
            return this.helpMessage;
        }else {
            return "Unkown command!";
        }
    }
    public String process2LengthCommand(String command, FileUtil fileUtil, TreeView FileTree) throws Exception{
        String[] commands = command.split(" ");
        //用户要进行的操作
        String commandOperation = commands[0];
        String commandContent = commands[1];


        if(!this.isIllegalPath(commandContent)) {
            //返回错误信息
            return "Illegal Argument : " + commandContent;
        }else if(this.isSame(commandOperation,"create")) {
            int result = fileUtil.createFile(commandContent, "x=1\nend");
            if(result == 1){
                return "路径错误！";
            }else if(result == 2){
                return "磁盘空间已满！";
            }else if(result == 3){
                return "该路径下有同名文件！";
            }else{
                Refresh refresh = new Refresh(FileTree, fileUtil,diskViewer);
                refresh.refreshFiles();
                return "创建文件成功！";

            }
        }else if(this.isSame(commandOperation,"open")) {
            String fileContent = fileUtil.getFileContent(commandContent);
            System.out.println(fileContent);
            PopFileContent popFileContent = new PopFileContent(fileContent,fileUtil,commandContent,diskViewer);
            return "打开文件成功！";
        }else if(this.isSame(commandOperation,"delete")) {
            boolean result = fileUtil.deleteFile(commandContent);
            if(result){
                Refresh refresh = new Refresh(FileTree, fileUtil,diskViewer);
                refresh.refreshFiles();
                return "删除文件成功！";
            }else
                return "路径错误！";
        }else if(this.isSame(commandOperation,"close")) {
            Refresh refresh = new Refresh(FileTree, fileUtil,diskViewer);
            refresh.refreshFiles();
            return "关闭文件成功！";
        }else if(this.isSame(commandOperation,"mkdir")) {
            int result;
            result = fileUtil.makeDirectory(commandContent);
            if(result == 1){
                return "路径错误！";
            }else if(result == 2){
                return "目录空间已满";
            }else{
                Refresh refresh = new Refresh(FileTree, fileUtil,diskViewer);
                refresh.refreshFiles();
                return "创建文件夹成功！";
            }
        }else if(this.isSame(commandOperation,"rmdir")) {
//            System.out.println("删除文件夹成功！");
            boolean result = fileUtil.removeDirectory(commandContent);
            if(result){
                Refresh refresh = new Refresh(FileTree, fileUtil,diskViewer);
                refresh.refreshFiles();
                return "删除文件夹成功！";
            }else {
                return "删除文件夹失败！";
            }
        } else{
            return "Unkown command!";
        }
    }

    //复制文件
    public String process3LengthCommand(String command, FileUtil fileUtil,TreeView FileTree) throws Exception{
        String[] commands = command.split(" ");
        String commandOperation = commands[0];
        String srcPath = commands[1];
        String destPath = commands[2];
        if(this.isSame(commandOperation,"copy")){
            int result = fileUtil.copyFile(srcPath,destPath);
            if(result == 1){
                return "路径错误！";
            }else if(result == 2){
                return "磁盘空间已满！";
            }else if(result == 3){
                return "该路径下有同名文件！";
            }else{
                Refresh refresh = new Refresh(FileTree, fileUtil,diskViewer);
                refresh.refreshFiles();
                return "复制文件成功！";
            }
        }else {
            return "Unkown command!";
        }
    }



    //判断是否为合法路径
    public boolean isIllegalPath(String path) {
        //如果以 / 开头，则为合法路径
        return path.startsWith("/");
    }
    public int getSegmentNumber(String command) {
        return command.split(" ").length;
    }
    public boolean isSame(String command, String systemCommand) {
        return command.equals(systemCommand);
    }
    //输入help之后展示的内容，帮助用户快速上手命令行操作文件
    private String initHelpMessage() {
        StringBuilder sb = new StringBuilder();
        sb.append("-help\n");
        sb.append("\t查看帮助信息\n");
//        sb.append("-aios\n");
//        sb.append("\t自动演示\n");
//        sb.append("-format\n");
//        sb.append("\t格式化磁盘\n");
        sb.append("-create /a/b\n");
        sb.append("\t根据路径创建文件\n");
        sb.append("-open /a/b\n");
        sb.append("\t打开文件\n");
//        sb.append("-close\n");
//        sb.append("\t关闭文件\n");
        sb.append("-delete\n");
        sb.append("\t删除文件\n");
//        sb.append("-type\n");
//        sb.append("\t显示文件内容\n");
        sb.append("-mkdir\n");
        sb.append("\t创建文件夹\n");
//        sb.append("-list\n");
//        sb.append("\t查看子文件夹\n");
        sb.append("-rmdir\n");
        sb.append("\t删除文件夹\n");
        sb.append("-copy\n");
        sb.append("\t复制文件");
        return sb.toString();
    }
}
